
var regeneratorRuntime = require("../../lib/js/runtime-module.js")
var cf = require("../../config.js");
import Request from "../../utils/request.js";
import Serv from './Serv';
const pageManager = require('../../lib/js/pageManager');
var webim = global.webim;
var app = getApp();
// pages/index/index.js
Page({
  data: {
    localCity: '定位中...',
    activeData: [],
    newsData: [],
    advertData: [],
    windowDate: [],
    hidden: true,
    floorstatus: true,
    scrollTop: 0,
    ossHost: cf.config.ossHost
  },
  /**
  * 生命周期函数--监听页面加载
  */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '金科神器',
    })

  }
  ,
  /**
   * 选择城市
   */
  changeCity: function () {
    wx.navigateTo({
      url: '/pages/switchCity/switchCity'
    })
  },
  getActive: function () {
    const that = this;
    Request({
      url: "app/index/houseActivity",
      type: "POST",
      data: {
        cityCode: app.globalData.cityData.cityadcode || "",
      },
    }).then((res) => {
      if (res.success) {
        that.setData({
          activeData: res.rows
        })
      }
    })
  },
  //跳转活动
  onActivity: function (e) {
    let { channel, businessId, contentId } = e.currentTarget.dataset;
    if (channel !== 4) {
      wx.navigateTo({
        url: `/subPackages2/pages/findHouse/activity/apply/apply?contentId=${contentId}&productId=${businessId}`
      });
    } else {
      wx.navigateTo({
        url: `/subPackages/pages/giftManage/giftDetail/giftDetail?giftId=${contentId}`
      });
    }
  },
  //跳转资讯
  onInformation: function (e) {
    let { channel, name, contentId } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/subPackages/pages/InformationDetail/InformationDetail?id=${contentId}&name=${name}`
    });
  },
  getNews: function () {
    const that = this;
    Request({
      url: "app/index/info",
      type: "POST",
      data: {
        cityCode: app.globalData.cityData.cityadcode || "",
      },
      headers: {
        "Content-type": "application/json"
      },
    }).then((res) => {
      if (res.success) {
        let date = []
        for (var i = 0; i < res.rows.length; i++) {
          date.push(res.rows[i].createTime)
        }
        let date1 = date.map(function (item) {
          return item.replace(/-/g, '.').substr(0, item.length - 8)
        })
        that.setData({
          newsData: res.rows.slice(0, 3),
          newsDataTotal: res.total,
          Date: date1
        })

      }
    })
  },
  toBulidingDetail: function (e) {
    let businessId = e.currentTarget.dataset.businessid;
    wx.navigateTo({ url: `/subPackages/pages/buildingDetail/buildingDetail?productId=${businessId}` })
  },
  userInfo: function () {
    let authName = wx.getStorageSync('authName')
    if (!authName) return
    Request({
      url: "app/user/queryUserInfo",
      type: "POST",
      data: {
        appId: 1,
      },
    }).then((res) => {
      wx.setStorageSync('userInfoData', res.data)
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow(options) {
    let ctx = this;
    let authName = wx.getStorageSync('authName');
    const { cityData } = app.globalData;
    if (JSON.stringify(cityData) !== "{}" && cityData !== null && cityData.city) {
      ctx.setData({
        localCity: app.globalData.cityData.city,
        cityData: app.globalData.cityData
      })
      ctx.getHomeData();
    } else {
      app.employIdCallback = res => {
        if (res) {
          ctx.setData({
            localCity: res.city,
            cityData: res
          })
          ctx.getHomeData();
        }
      }
    }
    if (authName) {
      let that = this;
      app.getUserIMSign(() => {
        that.getRecentContactList();
      })

    }
  },
  goGram(){
    wx.navigateToMiniProgram({
      appId: 'wx802463e13485158c',
      path: 'pages/home/home?scene=18622063-5-1-45',
      extraData: {
      },
      success(res) {
        // 打开成功
      }
    })
  },
  //获取洽谈室数据
  getRecentContactList(){
    let openId = wx.getStorageSync('openId'),loadRecentContactList = null,that = this;
    let recentParam = { openid: openId, start: 1, limit: 11, type: 1 }
    loadRecentContactList = Serv.queryMsgList(recentParam)
    app.getTotalUnreadNum(function () { }, that);
    pageManager.putData('loadRecentContactList', loadRecentContactList);
  },
  //获取个人名片
  goLoad() {
    const findByMemberOpenId = Serv.findByMemberOpenId({
      cusmallToken: wx.getStorageSync('cusmallToken'),
      start: 0,
      limit: 10
    })
    pageManager.putData('findByMemberOpenId', findByMemberOpenId);
    wx.navigateTo({ url: '/pages/load/load' })
  },
  getHomeData: function () {
    this.getRecommend();
    this.getNews();
    this.getActive();
    this.getSwiper();
    this.userInfo();
    this.getAdvertising(2, 1)
    this.getAdvertising(1, 1)
  },
  routergo: function (e) {
    let { aid, link } = e.currentTarget.dataset
    let url = `/${link}`.replace('//', '/');
    if (url.indexOf("https") >= 0 || url.indexOf("http") >= 0 || url.indexOf('www') >= 0) {
      wx.navigateTo({
        url: `/subPackages/pages/outsizeLink/outsizeLink?H5Url=${url}`,
      })

    } else {

      wx.navigateTo({
        url,
      })

    }
  },
  router: function (e) {

    let { href, idx } = e.currentTarget.dataset
    let item = this.data.swiperImgs[idx] || {}
    //href = `/${href}`.replace('//','/');
    if (href) {
      if (item.isOutLink) {//外链
        href = `/subPackages2/pages/h5/h5?href=${encodeURIComponent(item.href)}`
      } else {//内链
        href = `/${href}`.replace('//', '/')
      }
      wx.navigateTo({
        url: href,
      })
    }
  },
  getAdvertising: function (type, location = 1) {
    const that = this;
    Request({
      url: "app/advertisement/page",
      type: "POST",
      data: {
        type: type,
        location: location,
        cityCode: app && app.globalData.cityData.cityadcode,
        productType: 2
      },
    }).then((res) => {
      if (res.success) {
        res.rows.map((item, idx) => {
          item.img = item.imgUrl.split(',')[0]
          item.simg = item.imgUrl.split(',')[1]
        })
        if (type == 2) {
          that.setData({
            advertData: res.rows
          })
        } else {
          if (res.rows.length > 0) {
            if (wx.getStorageSync('noModal')) {
              return false
            }
            that.setData({
              windowDate: res.rows,
              hidden: false
            })
          }
        }
      }
    })
  },
  //不再提示
  noPrompt: function () {
    this.setData({
      checked: !this.data.checked,
      num: this.data.num + 1
    })
  },
  //关闭弹窗
  close: function () {
    wx.setStorage({
      key: "noModal",
      data: { noModal: true, type: 1, },
    })
    this.setData({
      hidden: true
    })
  },
  //广告跳转
  toAdvertisement: function (e) {
    let url = e.currentTarget.dataset.url
    url = `/${url}`.replace('//', '/')
    if (url.indexOf("http") > 0 || url.indexOf('www') > 0) {
      wx.navigateTo({
        url: `/subPackages/pages/outsizeLink/advertisement?H5Url=${url}`,
      })
    } else {
      wx.navigateTo({
        url: url,
      })
    }
  },
  //获取轮播图
  getSwiper: function () {
    const that = this;
    Request({
      //url:`http://10.73.158.46:8089/app/slideshow/query`,
      url: "app/slideshow/query",
      type: "POST",
      data: {
        type: 2,
        cityCode: app.globalData.cityData.cityadcode,
      }
    }).then((data) => {
      let rows = data.rows || []
      rows.map((item) => {
        item.isOutLink = false
        if (/((http|ftp|https):\/\/)/.test(item.href)) {
          item.isOutLink = true
        }
      })
      that.setData({
        swiperImgs: data && data.rows || []
      })
    })
  },
  // 推荐楼盘接口请求
  getRecommend: function () {
    const that = this;
    Request({
      url: "app/HomeRecommend/queryList",
      type: "POST",
      data: {
        cityCode: app.globalData.cityData && app.globalData.cityData.cityadcode || "",
      },
    }).then((res) => {
      if (res.success) {
        res.rows.map(item => {
          if (item.tag && item.tag.length > 3) {
            item.tag = item.tag.slice(0, 3)
          }
        })
        that.setData({
          recommendData: res.rows,
        })
      }
    })
  },
  onPageScroll: function (e) {
    this.setData({
      scrollTop: e.scrollTop
    })
  }
})
